
var millName; //染厂名称
var m_ClassName;
var m_ClassID;
var openID;


var code = getQueryString("code");

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function getopenid() {
    console.log(
        'https://www.xdfznh.club/wxpadbe/switch/oao/'
        + code);

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/switch/oao/'
            + code,
        type: 'get',
        dataType: 'json',
        // 成功回调
        success: function (data) {
            console.log('请求成功');
            openID = data.data;
            console.log('openID=' + openID);
            getmill();
        },
        //失败回调
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            //查看错误信息
            console.log('失败回调');
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    })
}

//获取微信号对应的染厂名称和班组
function getmill() {

    console.log('https://www.xdfznh.club/wxpadbe/user/selectByOpenId/'
        + openID);

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/user/selectByOpenId/'
            + openID,

        type: 'get',
        dataType: 'json',
        success: function (data) {

            millName = data.user_danwei;
            m_ClassName = data.class_name;
            m_ClassID = data.class_sid;

            if (m_ClassName.length>0){
                getData();
                getData1();
                getsum();
            }else{
                alert('当前用户无班组设置，不允许查看！');
                return;
            }

        }
    })
};


$(function () {

    console.log('code=' + code);
    getopenid();

});


function getData() {

    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/execute/query/'
            + millName + '/'
            + "call classProcessdetail('"
            + m_ClassName
            + "','1')",

        type: 'get',
        dataType: 'json',
        success: function (data) {

            $('#waterdetai').datagrid('loadData', data);

        }

    })

}

function getData1() {
    $.ajax({
        url: 'https://www.xdfznh.club/wxpadbe/execute/query/'
            + millName + '/'
            + "call classProcessdetail('"
            + m_ClassName
            + "','2')",

        type: 'get',
        dataType: 'json',
        success: function (data) {

            $('#waterdetai01').datagrid('loadData', data);

        }

    })
}



function  getsum() {
    //成品
    $('#waterdetai').datagrid({
        showFooter: true,
        onLoadSuccess: function (data) {
            data = data.rows;
            var qty = 0;
            var ps = 0;


            for (let i = 0; i < data.length; i++) {
                qty += data[i].B_Qty ? parseFloat(data[i].B_Qty) : 0;
                ps += data[i].B_PiShu ? parseFloat(data[i].B_PiShu) : 0;

            }
            $(this).datagrid('reloadFooter', [{
                vatNum: '合计',
                B_PiShu: ps.toFixed(0),
                B_Qty: qty.toFixed(1),
            }])
        }
    });

    //白坯
    $('#waterdetai01').datagrid({
        showFooter: true,
        onLoadSuccess: function (data) {
            data = data.rows;
            var qty = 0;
            var ps = 0;


            for (let i = 0; i < data.length; i++) {
                qty += data[i].B_Qty ? parseFloat(data[i].B_Qty) : 0;
                ps += data[i].B_PiShu ? parseFloat(data[i].B_PiShu) : 0;

            }
            $(this).datagrid('reloadFooter', [{
                B_ProcessName: '合计',
                B_PiShu: ps.toFixed(0),
                B_Qty: qty.toFixed(1),
            }])
        }
    });
}